using System.Collections;
using _ReplaceString_.UI.Elements;
using _ReplaceString_.UI.Styles;
using log4net.Util;
using RSCore;
using Terraria.ModLoader;

namespace _ReplaceString_.UI;

public class UISandBox : ModSystem
{
	private SandBoxLayer _layer = new SandBoxLayer();

	public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
	{
		layers.Insert(0, _layer);
	}

	private int _timer = 0;

	public override void PostUpdateEverything()
	{
		const int ResetTick = 60;
		if (_timer == 0)
		{
			_layer.Clear();
			foreach (var ui in GenerateUI())
			{
				_layer.Add(ui);
			}
		}
		_timer++;
		if (_timer == ResetTick)
		{
			_timer = 0;
		}
	}

	public static List<UIElement> GenerateUI()
	{
		var results = new List<UIElement>();
		var ui = new UIBase()
		{
			Width = new(100, 0),
			Height = new(100, 0),
			BackgroundColor = Color.White,
		};
		ui.AddStyle(UIBackground.Rect);
		results.Add(ui);

		var hidden = new UIBase()
		{
			Width = new(0, 1),
			Height = new StyleDimension(100,0),
		};
		hidden.AddStyle(UIScroll.Window);
		ui.Append(hidden);
		var background = new UIBase()
		{
			//Left = new(-1,0),
			//Top = new(-1,0),
			Width = new(0,1),
			Height = new(0,1),
			BackgroundColor = Color.Black
		};
		background.AddStyle(UIBackground.Rect);
		hidden.Append(background);

		var b = new UIBase()
		{
			Width = new(0, 1),
			Height = new StyleDimension(500, 0),
			Top = new StyleDimension(500, 0),
			BackgroundColor = Color.Blue
		};
		b.AddStyle(UIBackground.Rect);
		//ui.Append(b);
		return results;
	}

	public override void UpdateUI(GameTime gameTime)
	{
		_layer.Interface.Update(gameTime);
	}

	public class SandBoxLayer : GameInterfaceLayer, IList<UIElement>
	{
		public UserInterface Interface = new();

		public SandBoxLayer()
			: base("SandBox", InterfaceScaleType.UI)
		{
			Interface.SetState(_baseElement);
		}

		public override bool DrawSelf()
		{
			foreach (var child in Children)
			{
				child.Activate();
			}
			_baseElement.Recalculate();
			Vector2 size = default;
			foreach (var child in this)
			{
				size.X = Math.Max(size.X, child.Width.Pixels);
				size.Y = Math.Max(size.Y, child.Height.Pixels);
			}
			_baseElement.Left.Pixels = Main.screenWidth / 2 - size.X / 2;
			_baseElement.Top.Pixels = Main.screenHeight / 2 - size.Y / 2;
			_baseElement.Width = new StyleDimension(size.X, 0);
			_baseElement.Height = new StyleDimension(size.Y, 0);
			_baseElement.Recalculate();
			_baseElement._innerDimensions = _baseElement._innerDimensions.Fix();
			foreach(var child in _baseElement.Children)
			{
				child.Recalculate();
			}
			Interface.Draw(Main.spriteBatch, Main.gameTimeCache);
			return true;
		}

		private UIState _baseElement = new();

		#region IList Implements

		public int Count => Children.Count;

		public bool IsReadOnly => Children.IsReadOnly;

		public UIElement this[int index] { get => Children[index]; set => Children[index] = value; }

		public void Add(UIElement item)
		{
			_baseElement.Append(item);
		}

		public void Clear()
		{
			Children.Clear();
		}

		public bool Contains(UIElement item)
		{
			return Children.Contains(item);
		}

		public void CopyTo(UIElement[] array, int arrayIndex)
		{
			Children.CopyTo(array, arrayIndex);
		}

		public IEnumerator<UIElement> GetEnumerator()
		{
			return Children.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return ((IEnumerable)Children).GetEnumerator();
		}

		public int IndexOf(UIElement item)
		{
			return Children.IndexOf(item);
		}

		public void Insert(int index, UIElement item)
		{
			Children.Insert(index, item);
		}

		public bool Remove(UIElement item)
		{
			return Children.Remove(item);
		}

		public void RemoveAt(int index)
		{
			Children.RemoveAt(index);
		}

		private IList<UIElement> Children => _baseElement.Elements;

		#endregion IList Implements
	}
}